package org.shj.spark.operator;

import java.util.Arrays;
import java.util.List;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;

public class ReduceOperator {

	public static void main(String[] args) {
		SparkConf conf = new SparkConf().setMaster("local").setAppName("ReduceOperator");
		JavaSparkContext sc = new JavaSparkContext(conf);
		
		List<Integer> list = Arrays.asList(1,2,3,4,5);
		JavaRDD<Integer> numberRDD = sc.parallelize(list);
		
		//reduce操作的原理：首先将第一个和第二个元素传入call方法，计算
		//一个结果，接着把结果再和后面的元素依次累加，以此类推
		//注：算子里面的方法是在集群中运行的，返回的值是返回给driver的
		int sum = numberRDD.reduce(new Function2<Integer, Integer, Integer>(){
			private static final long serialVersionUID = 899049217229157239L;

			@Override
			public Integer call(Integer v1, Integer v2) throws Exception {
				return v1 + v2;
			}
			
		});
		
		System.out.println(sum);
		sc.close();

	}

}
